Skip to content

Conversation

@edsiper
Copy link
Member

@edsiper edsiper commented Nov 9, 2025

Fixes an issue when resolving input instance names for direct routing context


Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

Release Notes

  • New Features

    • Input routing now supports aliases for enhanced configuration flexibility
  • Bug Fixes

    • Improved input instance resolution with conflict detection to prevent routing conflicts
    • Enhanced handling of complex scenarios with multiple input instances

- Extended struct flb_input_routes to retain the parsed plugin name, track
  whether an alias was provided, and cache the resolved input instance for
  cleanup and reuse.

- Updated router configuration parsing and application to capture plugin names
  by default, resolve inputs by alias, internal instance name, or plugin type
  while avoiding reuse, and consume the enhanced resolver inside
  flb_router_apply_config.

Signed-off-by: Eduardo Silva <[email protected]>
Refreshed router tests to populate the new metadata, added
coverage proving distinct routing for duplicate plugin inputs
without aliases, registered the test case, and kept conditional
routing fixtures in sync.

Signed-off-by: Eduardo Silva <[email protected]>
@edsiper edsiper requested a review from cosmo0920 as a code owner November 9, 2025 13:18
@edsiper edsiper added this to the Fluent Bit v4.2 milestone Nov 9, 2025
@coderabbitai
Copy link

coderabbitai bot commented Nov 9, 2025

Walkthrough

The PR adds alias support to input routing in fluent-bit by introducing three new metadata fields to struct flb_input_routes (plugin_name, has_alias, and instance) and refactoring the input instance resolution logic to prioritize these fields with conflict detection.

Changes

Cohort / File(s) Summary
Router Header Definitions
include/fluent-bit/flb_router.h
Adds three new fields to struct flb_input_routes: plugin_name (flb_sds_t), has_alias (int), and instance (struct flb_input_instance *) to extend routing metadata.
Router Configuration Logic
src/flb_router_config.c
Adds alias-aware parsing in parse_input_section to extract and set alias fields; introduces input_instance_already_selected helper for conflict detection; refactors find_input_instance signature and logic to resolve instances by: existing instance pointer, alias match, name match (exact/prefix), and plugin name, with conflict checks throughout.
Router Tests
tests/internal/conditional_routing.c, tests/internal/router_config.c
Initializes and destroys new plugin_name and has_alias fields in test setup; adds test cases apply_config_uses_input_alias and apply_config_distinct_instances_without_alias to verify alias routing and multi-instance handling.

Sequence Diagram

sequenceDiagram
    actor Config
    participant find_input_instance
    participant resolve_logic

    Config->>find_input_instance: find_input_instance(config, routes)
    
    rect rgb(200, 220, 255)
    Note over find_input_instance: Resolution Priority Order
    alt routes->instance already set
        find_input_instance->>resolve_logic: return existing instance
    else has_alias
        find_input_instance->>resolve_logic: match by alias + check_conflicts
    else has input_name
        find_input_instance->>resolve_logic: exact match, then prefix match
    else has plugin_name
        find_input_instance->>resolve_logic: match by plugin_name
    else
        find_input_instance->>resolve_logic: return NULL
    end
    end
    
    resolve_logic-->>find_input_instance: flb_input_instance or NULL
    find_input_instance-->>Config: resolved instance
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Focus areas requiring extra attention:
    • Conflict detection logic in input_instance_already_selected and its integration into the instance resolution path
    • Resolution priority order in find_input_instance and correctness of each matching strategy (alias, name, plugin_name)
    • New test cases to verify alias and multi-instance scenarios work as intended
    • Error handling paths when allocating/destroying new fields in parse_input_section

Possibly related PRs

Suggested reviewers

  • koleini

Poem

🐰 Aliases bloom where routes once crossed,
No instance lost to conflict's frost—
Each input finds its truest name,
By alias, plugin, or by fame!
The fluent hop skips merrily on. 🌟

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: extending input name resolution logic in the routing system to support safer direct route paths via alias awareness and improved instance matching.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch routing-resolve-input-name

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c26a2d1 and 6c80f58.

📒 Files selected for processing (4)
  • include/fluent-bit/flb_router.h (1 hunks)
  • src/flb_router_config.c (6 hunks)
  • tests/internal/conditional_routing.c (2 hunks)
  • tests/internal/router_config.c (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
tests/internal/router_config.c (3)
src/flb_sds.c (2)
  • flb_sds_create (78-90)
  • flb_sds_destroy (389-399)
src/flb_router_config.c (1)
  • flb_router_apply_config (1435-1526)
src/flb_router.c (1)
  • flb_router_exit (274-301)
src/flb_router_config.c (4)
src/flb_sds.c (1)
  • flb_sds_destroy (389-399)
include/fluent-bit/flb_mem.h (1)
  • flb_free (126-128)
lib/cfl/src/cfl_kvlist.c (1)
  • cfl_kvlist_fetch (418-421)
lib/cfl/src/cfl_sds.c (1)
  • cfl_sds_len (88-91)
tests/internal/conditional_routing.c (1)
src/flb_sds.c (2)
  • flb_sds_create (78-90)
  • flb_sds_destroy (389-399)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (31)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit (Arm64), amd64_arm64, -DCMAKE_SYSTEM_NAME=Windows -DCMA...
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 64bit, x64, x64-windows-static, 3.31.6)
  • GitHub Check: pr-windows-build / call-build-windows-package (Windows 32bit, x86, x86-windows-static, 3.31.6)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_COMPILER_STRICT_POINTER_TYPES=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_ARROW=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=Off, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_THREAD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SIMD=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SANITIZE_MEMORY=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_COVERAGE=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_ADDRESS=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DSANITIZE_UNDEFINED=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_SMALL=On, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=Off, 3.31.6, gcc, g++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, clang, clang++)
  • GitHub Check: run-ubuntu-unit-tests (-DFLB_JEMALLOC=On, 3.31.6, gcc, g++)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-22.04, clang-12)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, clang, clang++, ubuntu-24.04, clang-14)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-24.04, clang-14)
  • GitHub Check: PR - fuzzing test
  • GitHub Check: pr-compile-without-cxx (3.31.6)
  • GitHub Check: pr-compile-system-libs (-DFLB_PREFER_SYSTEM_LIBS=On, 3.31.6, gcc, g++, ubuntu-22.04, clang-12)
  • GitHub Check: pr-compile-centos-7
🔇 Additional comments (11)
include/fluent-bit/flb_router.h (1)

153-155: LGTM! Clean struct extension for alias-aware routing.

The three new fields properly extend struct flb_input_routes to support alias-based input resolution with conflict detection. The additions are minimal and focused.

tests/internal/conditional_routing.c (1)

976-976: LGTM! Proper cleanup of plugin_name.

The cleanup correctly pairs with the initialization at line 886, ensuring no memory leaks.

tests/internal/router_config.c (3)

1166-1167: LGTM! Consistent initialization and cleanup across existing tests.

The updates properly initialize the new plugin_name and has_alias fields and clean them up. The pattern is consistent across all modified test functions.

Also applies to: 1193-1193, 1216-1217, 1242-1242


1247-1388: LGTM! Comprehensive test for alias-based input routing.

This test properly validates that when has_alias = FLB_TRUE, the router matches input instances by their alias field. The test setup is thorough and includes proper verification of route-to-output mappings.


1390-1530: LGTM! Excellent test for conflict detection with multiple instances.

This test validates a crucial scenario: when multiple input instances share the same plugin type without aliases, the conflict detection mechanism (via input_instance_already_selected) ensures each route is assigned to a distinct instance. Lines 1515-1516 properly verify the instance assignments.

src/flb_router_config.c (6)

391-393: LGTM! Proper cleanup for the new plugin_name field.

The cleanup correctly mirrors the pattern used for input_name and prevents memory leaks.


1138-1159: LGTM! Alias handling logic is well-structured.

The code properly initializes the new fields and handles alias resolution:

  • plugin_name is always set to the actual plugin name
  • input_name is set to alias if present and non-empty, otherwise to plugin name
  • has_alias flag correctly tracks which scenario applies
  • Error handling at line 1157 properly calls input_routes_destroy(input) which cleans up both allocated fields

The non-empty check at line 1149 (cfl_sds_len(alias_var->data.as_string) > 0) appropriately prevents empty aliases.


1247-1271: LGTM! Well-designed conflict detection helper.

The input_instance_already_selected function correctly prevents multiple routes from selecting the same input instance. The logic properly skips the current route and checks all others for conflicts.


1336-1352: LGTM! Partial matching enables plugin-type-based routing.

The prefix matching logic correctly supports routing to plugin instances by their type (e.g., "dummy" matching "dummy.0"). The conflict detection ensures unique instance assignment when multiple instances share the same prefix.


1456-1456: LGTM! Call site properly updated for new signature.

The function call correctly passes the entire input_routes structure, enabling access to plugin_name, has_alias, and cached instance fields.


1288-1334: Clarify alias fallback semantics with intentional early return.

When has_alias is TRUE, if alias matching fails to find an input instance, the function falls through to try other matching strategies (exact name, plugin name). This means an input with name "foo" could be matched when searching for alias "foo", which may be unintended.

The tests do not exercise the has_alias=TRUE path and no code comments explain this fallback behavior, making the design intent unclear.

Suggested fix:
Add an early return after the alias matching block when has_alias is TRUE to enforce stricter alias-based routing:

    if (routes->has_alias && routes->input_name) {
        mk_list_foreach(head, &config->inputs) {
            ins = mk_list_entry(head, struct flb_input_instance, _head);

            if (!ins->p || !ins->alias) {
                continue;
            }

            if (strcmp(ins->alias, routes->input_name) == 0 &&
                input_instance_already_selected(config, routes, ins) == FLB_FALSE) {
                routes->instance = ins;
                return ins;
            }
        }
+       return NULL;
    }

Verify whether the current fallback behavior is intentional or if alias-based routing should fail fast when the alias is not found.

Comment on lines +886 to +887
input_routes->plugin_name = flb_sds_create("tail");
input_routes->has_alias = FLB_FALSE;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

LGTM! Test setup properly initializes new routing fields.

The initialization correctly sets plugin_name to "tail" and explicitly disables alias usage with has_alias = FLB_FALSE, which aligns with the test scenario.

However, verify that if flb_sds_create("tail") fails (returns NULL), subsequent code handles it gracefully. The test doesn't check for NULL return from flb_sds_create.


🏁 Script executed:

#!/bin/bash
# Check if there's error handling for plugin_name allocation failures in tests
rg -n -A5 -B2 'plugin_name = flb_sds_create' tests/internal/conditional_routing.c

Length of output: 339



---

🏁 Script executed:

```shell
#!/bin/bash
# Get the full setup_conditional_routes function to see error handling pattern
rg -n -B10 'input_routes->plugin_name = flb_sds_create' tests/internal/conditional_routing.c | head -50

Length of output: 672


🏁 Script executed:

#!/bin/bash
# Check the complete function definition and any error handling
rg -n -A30 'setup_conditional_routes' tests/internal/conditional_routing.c | head -80

Length of output: 3586


🏁 Script executed:

#!/bin/bash
# Look for NULL checks on input_routes fields after initialization
rg -n -A15 'input_routes->has_alias = FLB_FALSE' tests/internal/conditional_routing.c

Length of output: 677



---

🏁 Script executed:

```shell
#!/bin/bash
# Find the exact function signature/definition
rg -n -B5 'setup_conditional_routes\(' tests/internal/conditional_routing.c | grep -A5 'setup_conditional_routes'

Length of output: 1144


🏁 Script executed:

#!/bin/bash
# Check if other test setup helpers validate allocations
rg -n -A10 'flb_sds_create' tests/internal/conditional_routing.c | grep -B2 -A3 'NULL'

Length of output: 267


🏁 Script executed:

#!/bin/bash
# Look for the actual setup_conditional_routes function definition (not calls)
ast-grep --pattern 'void setup_conditional_routes($_) { $$$ }'

Length of output: 0



---

🏁 Script executed:

```shell
#!/bin/bash
# Get the complete setup_conditional_routes function body
rg -n -A60 'static void setup_conditional_routes' tests/internal/conditional_routing.c | head -100

Length of output: 2982


Add NULL validation for allocation failures in setup_conditional_routes.

The function makes 7 flb_sds_create calls (lines 885–886, 893, 900, 907, 914, 921, 928) without checking for NULL returns. This is inconsistent with the validation pattern used elsewhere in the test file (line 229: TEST_CHECK(rule->field != NULL && rule->op != NULL && rule->value != NULL);). If any allocation fails, the function will pass NULL pointers to subsequent operations like cfl_list_add, causing undefined behavior. Add TEST_CHECK assertions to validate all allocations before using them.

🤖 Prompt for AI Agents
In tests/internal/conditional_routing.c around lines 885–928, multiple
flb_sds_create calls (lines 885–886, 893, 900, 907, 914, 921, 928) are used
without NULL checks; add TEST_CHECK assertions immediately after each
flb_sds_create to verify the returned pointer is not NULL before it is assigned
or used (e.g., TEST_CHECK(input_routes->plugin_name != NULL)); do this for every
allocation site so subsequent calls like cfl_list_add never receive NULL
pointers.

@edsiper edsiper merged commit db274a3 into master Nov 9, 2025
60 checks passed
@edsiper edsiper deleted the routing-resolve-input-name branch November 9, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants